package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.ApprovalProcess;
import com.nuoniu.sibanyun.service.crm.IApprovalProcessService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 审批流程 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/sys/approvalProcess")
@Api(tags = "审批流程")
public class ApprovalProcessController extends BaseController {

    @Autowired
    IApprovalProcessService approvalProcessService;

    /**
     * 获取审批流程列表
     * @author 小啊刚
     * @date 2020/7/9
     */
    @GetMapping("page")
    public HttpResult pageApprovalProcess(Page page, ApprovalProcess process) {
        return HttpResult.ok(approvalProcessService.pageApprovalProcess(page, process));
    }

    /**
     * 保存审批流程
     * @author 小啊刚
     * @date 2020/7/9
     */
    @PostMapping("save")
    public HttpResult saveProcess(@RequestBody ApprovalProcess process) {
        UserInfo userInfo = UserThreadLocal.get();
        if(process.getId() != null) {
            process.setUpdateDate(new Date());
            process.setUpdateUserId(userInfo.getUserId());
            process.setUpdateUserName(userInfo.getRealName());
        }else {
            process.setCompanyId(userInfo.getCompanyId());
            process.setCompanyName(userInfo.getCompanyName());
            process.setCreateUserId(userInfo.getUserId());
            process.setCreateUserName(userInfo.getRealName());
        }
        approvalProcessService.saveOrUpdate(process);
        return HttpResult.ok();
    }

    /**
     * 停用
     */
    @PostMapping(value = "disable")
    public HttpResult disableSelected(@RequestBody List<Long> ids) {
        for(Long id : ids) {
            ApprovalProcess process = new ApprovalProcess();
            process.setStatus(2);
            process.setId(id);
            approvalProcessService.updateById(process);
        }
        return HttpResult.ok("停用成功");
    }
}
